//@Time:2022/4/2 16:05
//@Author:aFun

package indi.csms_springboot.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.csms_springboot.domain.*;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Mapper
public interface TeacherMapper extends BaseMapper<Teacher> {

    @Select(
            "select * from course c JOIN tea2cou tc ON c.couId=tc.couId ${ew.customSqlSegment}"
    )
    public Page<Course> findCourseByPage(Page<Course> coursePage,
                                         @Param(Constants.WRAPPER) QueryWrapper<Course> wrapper);

    // 通过教师账号寻找到教师ID
    @Select(
            "select teaId from teacher where account='${account}'"
    )
    public Integer findByAccount(@Param("account")String account);

    // 通过课程ID找到课程
    @Select(
            "select * from course where couId=${couId}"
    )
    public Course findCourseByCouId(@Param("couId")Integer couId);

    @Select(
            "SELECT DISTINCT ss.ssId,ss.couId,c.couName,ss.teaId,ss.stuId,u.name as stuName,u.gender,ss.score " +
                    "FROM course c JOIN tea2cou tc JOIN stu2score ss JOIN student s JOIN USER u " +
                    "ON ss.couId=c.couId AND ss.stuId=s.stuId AND ss.teaId=tc.teaId " +
                    "AND s.account=u.account ${ew.customSqlSegment}"
    )
    public Page<StudentOrder> findStudentByPage(Page<Course> coursePage,
                                                @Param(Constants.WRAPPER) QueryWrapper<Course> wrapper);

    @Select(
            "SELECT DISTINCT c.couId,c.couName " +
                    "FROM course c JOIN tea2cou tc " +
                    "${ew.customSqlSegment}"
    )
    public Page<Course> findCourseForOpen(Page<Course> coursePage,
                                          @Param(Constants.WRAPPER) QueryWrapper<Course> wrapper);

    @Select(
            "select couId from tea2cou where teaId=${teaId}"
    )
    public Collection<Integer> findCouIdsByTeaId(@Param("teaId")Integer teaId);

    @Update(
            "update stu2score set score=${score} where ssid=${ssid}"
    )
    public boolean recordScore(@Param("score") int score,@Param("ssid") Integer ssid);


    @Insert(
            "insert into tea2cou values(null,${teaId},${couId})"
    )
    public boolean selectCourse(@Param("couId")Integer couId,@Param("teaId")Integer teaId);
}
